package cn.org.book.shop.web.controller.article;

import cn.org.book.shop.article.domain.ApArticle;
import cn.org.book.shop.article.domain.ApDownload;
import cn.org.book.shop.article.service.IApArticleService;
import cn.org.book.shop.article.service.IApDownloadService;
import cn.org.book.shop.common.annotation.Log;
import cn.org.book.shop.common.core.controller.BaseController;
import cn.org.book.shop.common.core.domain.AjaxResult;
import cn.org.book.shop.common.core.page.TableDataInfo;
import cn.org.book.shop.common.enums.BusinessType;
import cn.org.book.shop.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * APP用户频道信息Controller
 *
 * @author julywhj
 * @date 2020-11-17
 */
@RestController
@RequestMapping("/ap/download")
public class ApDownloadController extends BaseController {
    @Autowired
    private IApDownloadService apDownloadService;
    @Resource
    private IApArticleService apArticleService;

    /**
     * 查询APP用户频道信息列表
     */
    @PreAuthorize("@ss.hasPermi('ap:download:list')")
    @GetMapping("/list")
    public TableDataInfo list(ApDownload apDownload) {
        startPage();
        List<ApDownload> list = apDownloadService.selectApDownloadList(apDownload);
        return getDataTable(list);
    }

    /**
     * 导出APP用户频道信息列表
     */
    @PreAuthorize("@ss.hasPermi('ap:download:export')")
    @Log(title = "APP用户频道信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ApDownload apDownload) {
        List<ApDownload> list = apDownloadService.selectApDownloadList(apDownload);
        ExcelUtil<ApDownload> util = new ExcelUtil<>(ApDownload.class);
        return util.exportExcel(list, "download");
    }

    /**
     * 获取APP用户频道信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('ap:download:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(apDownloadService.selectApDownloadById(id));
    }

    /**
     * 新增APP用户频道信息
     */
    @PreAuthorize("@ss.hasPermi('ap:download:add')")
    @Log(title = "APP用户频道信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApDownload apDownload) {
        ApArticle apArticle = apArticleService.selectApArticleById(apDownload.getArticleId());
        if (apArticle != null) {
            apArticle.setIsDownLoad(1);
            apArticleService.updateApArticle(apArticle);
            apDownload.setName(apArticle.getTitle());
        }
        return toAjax(apDownloadService.insertApDownload(apDownload));
    }

    /**
     * 修改APP用户频道信息
     */
    @PreAuthorize("@ss.hasPermi('ap:download:edit')")
    @Log(title = "APP用户频道信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApDownload apDownload) {
        return toAjax(apDownloadService.updateApDownload(apDownload));
    }

    /**
     * 删除APP用户频道信息
     */
    @PreAuthorize("@ss.hasPermi('ap:download:remove')")
    @Log(title = "APP用户频道信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(apDownloadService.deleteApDownloadByIds(ids));
    }
}
